package models.data;

import com.avaje.ebean.CallableSql;
import com.avaje.ebean.Ebean;
import models.domain.MultipleChoice;
import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: bjornth
 * Date: 23.3.2013
 * Time: 16:59
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class Svor extends Model{

    @Id
    //@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "s_svor")
    public int svar_id;

    public Integer spurning_id;
    public String svor_texti;
    public Date time_create;

    public Date time_edit;

    public String person_create;
    public String person_edit;
    public int rettsvar;


    public Svor(Integer spurning_id, String svor_texti, Date time_create,
                String person_create, String person_edit, int rettsvar) {
        this.svar_id = createId();
        this.spurning_id = spurning_id;
        this.svor_texti = svor_texti;
        this.time_create = time_create;
        //this.time_edit = time_edit;
        this.person_create = person_create;
        this.person_edit = person_edit;
        this.rettsvar = rettsvar;
    }
    public Svor(Integer spurning_id, String svor_texti, String person_create,String person_edit, int rettSvar){
        this.spurning_id = spurning_id;
        this.svor_texti = svor_texti;
        this.person_create = person_create;
        this.person_edit = person_edit;
        this.rettsvar = rettSvar;

    }

    public static Integer createId(){
        List<Object> svor = Svor.find.findIds();

        Integer max = 1;
        for(Object verk : svor){
            if(Integer.valueOf(verk.toString()) > max) {
                max = Integer.valueOf(verk.toString());
            }
        }
        return (max+1);
    }

    public Svor(Integer spurning_id, String svor_texti, int rettsvar) {
        this.svar_id = createId();
        this.spurning_id = spurning_id;
        this.svor_texti = svor_texti;
        this.rettsvar = rettsvar;
    }

    public static Svor create(Integer spurning_id, String svor_texti,
                              String person_create, String person_edit, int rettsvar){

        Date date = new Date();
        Svor svor = new Svor(spurning_id, svor_texti, date,
                             person_create, person_edit, rettsvar);
        svor.save();
        return svor;
    }
    public static Svor createEydufylling(Integer spurning_id, String svor_texti, String person_create){
        Date date = new Date();
        Svor svor = new Svor(spurning_id,svor_texti,date,person_create,"",0);
        svor.save();
        return svor;

    }

    public static Svor changeSvar(Integer spurning_id,String svar){
        List<Svor> eydufyllingSvor = Svor.getSvorBySpurningID(36);
        Svor svarNew = eydufyllingSvor.get(0);
        svarNew.setTexti(svar);
        svarNew.save();
        return svarNew;
    }

    public static List<Svor> changeSingleChoiceAnswers(Integer spurning_id,String rightAnswer, String[] answers){
        List<Svor> svor = Svor.getSvorBySpurningID(spurning_id);
        for(Svor single:svor){
            if(single.getRettsvar()==1){
                String sql = "{call update_single_choice_answer(?,?)}";
                CallableSql cs = Ebean.createCallableSql(sql);
                cs.setParameter(1,single.getSvar_id());
                cs.setParameter(2,rightAnswer);
                Ebean.execute(cs);
                //svor.remove(single);
            }
        }
        int index=0;
        for(Svor single:svor){
            if(single.getRettsvar()==0){
                String sql = "{call update_single_choice_answer(?,?)}";
                CallableSql cs = Ebean.createCallableSql(sql);
                cs.setParameter(1,single.getSvar_id());
                cs.setParameter(2,answers[index]);
                Ebean.execute(cs);
                index++;
            }
        }
        return svor;
    }

    public static List<Svor> changeMultiChoiceAnswers(Integer spurning_id,String[] rightAnswers, String[] wrongAnswers){
        List<Svor> svor = Svor.getSvorBySpurningID(spurning_id);
        int indexRight = 0;
        for(Svor single:svor){
            if(single.getRettsvar()==1){
                String sql = "{call update_single_choice_answer(?,?)}";
                CallableSql cs = Ebean.createCallableSql(sql);
                cs.setParameter(1,single.getSvar_id());
                cs.setParameter(2,rightAnswers[indexRight]);
                Ebean.execute(cs);
                indexRight++;
                //svor.remove(single);
            }
        }
        int indexWrong=0;
        for(Svor single:svor){
            if(single.getRettsvar()==0){
                String sql = "{call update_single_choice_answer(?,?)}";
                CallableSql cs = Ebean.createCallableSql(sql);
                cs.setParameter(1,single.getSvar_id());
                cs.setParameter(2,wrongAnswers[indexWrong]);
                Ebean.execute(cs);
                indexWrong++;
            }
        }
        return svor;
    }


    public static Finder<Integer,Svor> find = new Finder<Integer,Svor>(
            Integer.class, Svor.class
    );

    public int getSvar_id() {
        return svar_id;
    }

    public void setSvar_id(int svar_id) {
        this.svar_id = svar_id;
    }

    public Integer getSpurning_id() {
        return spurning_id;
    }

    public void setSpurning_id(Integer spurning_id) {
        this.spurning_id = spurning_id;
    }

    public String getTexti() {
        return svor_texti;
    }

    public void setTexti(String texti) {
        this.svor_texti = svor_texti;
    }

    public Date getTime_create() {
        return time_create;
    }

    public void setTime_create(Date time_create) {
        this.time_create = time_create;
    }

    public Date getTime_edit() {
        return time_edit;
    }

    public void setTime_edit(Date time_edit) {
        this.time_edit = time_edit;
    }

    public String getPerson_create() {
        return person_create;
    }

    public void setPerson_create(String person_create) {
        this.person_create = person_create;
    }

    public String getPerson_edit() {
        return person_edit;
    }

    public void setPerson_edit(String person_edit) {
        this.person_edit = person_edit;
    }

    public int getRettsvar() {
        return rettsvar;
    }

    public void setRettsvar(int rettsvar) {
        this.rettsvar = rettsvar;
    }
    public static List<Svor> getSvorBySpurningID(int spurning_id){
        List<Svor> svor = Svor.find.where().eq("spurning_id", spurning_id).findList();
        Collections.sort(svor, new Comparator<Svor>() {
            @Override
            public int compare(Svor o1, Svor o2) {
                return Integer.compare(o1.getSvar_id(),o2.getSvar_id());
            }
        });
        return svor;
    }
    public static List<MultipleChoice> getCorrectAnswers(List<Integer> spurning_id){
        List<MultipleChoice> multipleChoices = new ArrayList<MultipleChoice>();
        for(Integer s : spurning_id){
           List<Svor> svor = Svor.getSvorBySpurningID(s);
            for(Svor sv: svor){
                if(sv.getRettsvar() == 1){
                    multipleChoices.add(new MultipleChoice(s,sv.getSvar_id()));
                }
            }
        }

        return multipleChoices;
    }
    public static List<MultipleChoice> getCorrectAnswersForBoth(List<Integer> spurning_id){
        List<MultipleChoice> multipleChoices = new ArrayList<MultipleChoice>();
        for(Integer s : spurning_id){
            List<Svor> svor = Svor.getSvorBySpurningID(s);
            int count = 0;
            for(Svor sv: svor){
                if(sv.getRettsvar() == 1){
                    count++;
                }
            }
            if(count == 1){
                for(Svor sv: svor){
                    if(sv.getRettsvar()==1){
                        multipleChoices.add(new MultipleChoice(s,sv.getSvar_id()));
                    }
                }
            }
            else{
                List<Integer> tempSvorID = new ArrayList<Integer>();
                for(Svor sv: svor){
                    if(sv.getRettsvar() == 1){
                      tempSvorID.add(sv.getSvar_id());
                    }
                }
                multipleChoices.add(new MultipleChoice(s,tempSvorID));
            }

        }

        return multipleChoices;
    }
}
